View Javadoc

1   /**
2    * Copyright 2008 WebPhotos
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.webphotos.locator;
17  
18  import java.util.Hashtable;
19  import javax.naming.Context;
20  import javax.naming.InitialContext;
21  import javax.naming.NamingException;
22  import org.apache.log4j.Logger;
23  
24  /**
25   *
26   * @author Guilherme
27   */
28  public class BasicEJBLocator {
29  
30  	/**
31       *
32       */
33      protected InitialContext cachedContext = null;
34  	/**
35       *
36       */
37      public static String EJB_URL_PREFIXES = "org.jboss.naming";
38  	/**
39       *
40       */
41      public static String EJB_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
42  	/**
43       *
44       */
45      public static String EJB_SERVER = "127.0.0.1:1099";
46  
47  	private static Logger logger = Logger.getLogger(BasicEJBLocator.class);
48  
49  	/**
50       *
51       */
52      public void initEJBContext() {
53  		try {
54  			cachedContext = (InitialContext) getRemoteEJBsInitialContext();
55  		} catch (NamingException e) {
56  			logger.error("Erro fatal com context de EJBs", e);
57  		}
58  	}
59  
60  	/**
61       *
62       * @return
63       */
64      public InitialContext getEJBContext() {
65  		if (cachedContext == null) {
66  			initEJBContext();
67  		}
68  		return cachedContext;
69  	}
70  
71  	/**
72       *
73       * @return
74       * @throws NamingException
75       */
76      public Context getRemoteEJBsInitialContext() throws NamingException {
77  		Hashtable<String, String> props = new Hashtable<String, String>();
78  		props.put(Context.INITIAL_CONTEXT_FACTORY, EJB_CONTEXT_FACTORY);
79  		props.put(Context.PROVIDER_URL, EJB_SERVER);
80  		props.put(Context.URL_PKG_PREFIXES, EJB_URL_PREFIXES);
81  		return new InitialContext(props);
82  	}
83  
84  }